<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Base extends Model
{
    //设置白名单
    protected $guarded =[];

    //递归 查询权限节点 层级关系
    public function treelevel($data,$pid=0,$html="--",$level=0){
        static $arr =[];
        foreach ($data as $value){
            if($value['pid'] == $pid){
                //重复一个字符多少次
//                $value['html']  = str_repeat($html,$level*2);
                $value['level'] = $level+1;
                $value['name']  = str_repeat($html,$level*2).$value['name'];
                $arr[]=$value;
                $this->treelevel($data,$value['id'],$html,$value['level']);
            }
        }
        return $arr;
    }
    //递归查询菜单
    public function  getMenuTree($data,$pid=0){
        $arr =[];
        foreach ($data as $value){
            //给的pid 是当前父级 的id 进行循环
            if($value['pid'] == $pid){
               //子集 的父id 是当前的id
                $ar['title']=$value['name'];
                $ar['href']= '';
                if($value['route_name']){
                    $ar['href']= route($value['route_name']);
                }
                $ar['icon']=$this->icon();
                $ar['target']="_self";
                $ar['child']= $this->getMenuTree($data,$value['id']);
                $arr[] = $ar;
            }
        }
        return $arr;
    }
    public function icon(){
        $d=  [
            "fa fa-address-book",
            "fa fa-lemon-o",
            "fa fa-dot-circle-o",
            "fa fa-adn",
            "fa fa-dashboard",
            "fa fa-rocket",
            "fa fa-meetup",
            "fa fa-slideshare",
            "fa fa-edit",
            "fa fa-arrow-up",
            "fa fa-home",
            "fa fa-arrow-up",
            "fa fa-window-maximize",
            "fa fa-gears",
            "fa fa-flag-o",
            "fa fa-snowflake-o",
            "fa fa-calendar",
        ];
      return  $d[rand(0,16)];
    }

}
